#include "vnc_computer_thread.h"


VNC_ComputerThread::VNC_ComputerThread (ResultFormatter& resFmt, ADCM_Computer& adcmComputer, wstring& user,
										wstring& password, wstring& domain,bool isLocal)
										: _adcmComputer(adcmComputer), _user(user),
										_password(password), _domain(domain), _resFormatter(resFmt),_isLocal(isLocal)
{
}

DWORD VNC_ComputerThread::Run(LPVOID arg)
{
	CM_ResultCollection cmRes;
	CM_ResultCollection cmDifRes;

	wstring aux(_adcmComputer.getCn());
	WMI_HResult hres;

	int cont = 0;

	do {	
		hres = bridget_communicator::getComputersResult(_adcmComputer.getIdComputer(), 
			cmRes,
			_user, 
			_password, aux,_domain, _isLocal);

	} while(cont++ <= 5 && FAILED(hres.getHRESULT()));

	// TODO hacer validacion
	hres = bridget_communicator::save(_adcmComputer.getIdComputer(), cmRes);
	
	if (FAILED(hres.getHRESULT()))
	{
		wcerr << "Error to save computer summary: " << _adcmComputer.getCn() << endl;
		return 0;
	}
	
	vector<WMI_Process*> vec = cmRes.getProcesses().getObjects();

	for (vector<WMI_Process*>::iterator iter = vec.begin(); iter != vec.end(); iter++)	
		wcout << "Process" << (*iter)->getCaption() << endl;	

	bridget_communicator::getDifferences(_adcmComputer.getIdComputer(), cmDifRes);
	
	_resFormatter.printResult(_adcmComputer, cmDifRes);

	return 0;
}
